package logic

import (
	"context"
	"fmt"

	"server_1.0/service/result/internal/svc"
	"server_1.0/service/result/model"
	"server_1.0/service/result/result"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInsertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertLogic {
	return &InsertLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InsertLogic) Insert(in *result.InsertReq) (*result.QReps, error) {
	code := util.Success
	newRes := model.Result{
		Etime: in.Time,
		Rsno:  in.Sno,
		Rpno:  in.Pno,
		Rans:  in.Ans,
	}
	fmt.Println(newRes)
	usersHandle := make([]*result.Reps, 0)
	count := 0
	_, err3 := l.svcCtx.ResultModel.Insert(l.ctx, &newRes)
	if err3 != nil {
		code = util.AddError
	}
	usersHandle = append(
		usersHandle,
		&result.Reps{
			Code: int64(code),
			Time: in.Time,
			Sno:  in.Sno,
			Pno:  in.Pno,
			Ans:  in.Ans,
		})
	count += 1
	return &result.QReps{
		Count: int64(count),
		QRs:   usersHandle,
	}, nil
}
